package cn.com.api.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.com.api.entity.ApiTmember;
import cn.com.api.entity.ApiU_advert;
import cn.com.api.entity.ApiU_advert_resource;
import cn.com.api.entity.ApiU_bonus;
import cn.com.api.entity.ApiU_bonus_detail;
import cn.com.api.service.ApiMemberService;
import cn.com.api.service.ApiU_advertService;
import cn.com.api.service.ApiU_bonusService;
import cn.com.api.service.ApiU_bonus_detailService;
import cn.com.api.service.ApiU_stat_keywordsService;
import cn.com.api.service.CheckService;

//import cn.com.web.tool.JsonUtil;
//import cn.com.web.tool.PageInfo;
//import cn.com.web.tool.Util;
//import cn.com.web.tool.Utils;

import cn.com.util.*;

/**
 * 广告模块api控制器
 * 
 * @return
 */

@Controller
@RequestMapping(Utils.SYS_API + "advert/")
public class ApiU_advertController {

	private static Logger logger = Logger.getLogger(ApiU_advertController.class);

	@Autowired
	private ApiU_advertService apiU_advertService;
	@Autowired
	private ApiU_bonusService apiU_bonusService;
	@Autowired
	private ApiU_bonus_detailService apiU_bonus_detailService;
	@Autowired
	private CheckService checkService;
	@Autowired
	private ApiMemberService apiMemberService;
	@Autowired
	private ApiU_stat_keywordsService apiU_stat_keywordsService;

	// 根据广告ID取广告图片或视频findU_advert_resourceByAdvertID
	@RequestMapping(value = "getU_advert_Resource")
	public void getU_advert_Resource(HttpServletRequest request, HttpServletResponse response, String advert_ID,
			String user_id) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			Map<String, String> params = new HashMap<String, String>();
			params.put("advert_ID", advert_ID);
			if (checkService.checkParams(params, map)) {
				List<ApiU_advert_resource> uAdvert_resourceList = this.apiU_advertService
						.findU_advert_resourceByAdvertID(advert_ID, user_id);
				ApiU_bonus bonus = this.apiU_bonusService.getBonusInfoByAdvertID(advert_ID);

				if (Util.isNull(uAdvert_resourceList)) {
					map.put(Utils.RESULT, Utils.RESULT_RC_NO_DATA);
					map.put(Utils.MESSAGE, "红包已被抢光");
					map.put("isGot", "0");
					map.put("BlessWords", bonus.getBless_word());
				} else {
					// 检查是否已领过红包
					ApiU_bonus_detail hasDetail = apiU_bonus_detailService.checkAvailableBonus(bonus.getId().toString(),
							user_id);
					if (Util.isNull(hasDetail)) {
						map.put("isGot", "0");
					} else {
						map.put("isGot", "1");
					}
					map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
					map.put(Utils.MESSAGE, "操作成功");
					map.put(Utils.DATAS, uAdvert_resourceList);
				}
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			map.put(Utils.RESULT, Utils.RESULT_RC_EXCEPTION);
			map.put(Utils.MESSAGE, "系统异常");
		}
		JsonUtil.printJson(response, JsonUtil.objectToJsonStr(map));
	}

	// 根据广告类型获取广告列表（非红包广告）
	@RequestMapping(value = "getPositionU_advert")
	public void getPositionU_advert(HttpServletRequest request, HttpServletResponse response, String user_id,
			String token, String position_id, PageInfo page) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			// String cUserId = user_id;
			Map<String, String> params = new HashMap<String, String>();
			// params.put("u_id", cUserId);
			// params.put("token", token);
			params.put("position_id", position_id);
			// checkService.checkToken(cUserId, token, map) &&
			if (checkService.checkParams(params, map)) {

				List<ApiU_advert> uAdvertList = this.apiU_advertService.findU_advertByPostionID(user_id,
						Integer.valueOf(position_id), page);

				map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
				map.put(Utils.MESSAGE, "获取完成");
				map.put(Utils.DATAS, uAdvertList);
				map.put(Utils.Page, page);
			}

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			map.put(Utils.RESULT, Utils.RESULT_RC_EXCEPTION);
			map.put(Utils.MESSAGE, "系统异常");
		}
		JsonUtil.printJson(response, JsonUtil.objectToJsonStr(map));
	}

	// 根据广告类型获取广告列表（红包广告）
	@RequestMapping(value = "getBonusU_advert")
	public void getBonusU_advert(HttpServletRequest request, HttpServletResponse response, String user_id, String token,
			String position_id, PageInfo page) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			Map<String, Object> params = new HashMap<String, Object>();

			params.put("position_id", position_id);

			if (!Util.isNull(user_id))// 登录用户
			{
				if (checkService.checkToken(user_id, token, map)) {
					params.put("user_id", user_id);
					List<ApiU_advert> uAdvertList = this.apiU_advertService.findBonus_AdvertByPostionIDListPage(params,
							page);

					map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
					map.put(Utils.MESSAGE, "获取完成");
					map.put(Utils.DATAS, uAdvertList);
					map.put(Utils.Page, page);
				}
			} else {
				List<ApiU_advert> uAdvertList = this.apiU_advertService.findBonus_AdvertByPostionIDListPage(params,
						page);

				map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
				map.put(Utils.MESSAGE, "获取完成");
				map.put(Utils.DATAS, uAdvertList);
				map.put(Utils.Page, page);
			}

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			map.put(Utils.RESULT, Utils.RESULT_RC_EXCEPTION);
			map.put(Utils.MESSAGE, "系统异常");
		}
		JsonUtil.printJson(response, JsonUtil.objectToJsonStr(map));
	}

	// 获取推荐红包广告
	@RequestMapping(value = "getInterestBonusU_advert")
	public void getInterestBonusU_advert(HttpServletRequest request, HttpServletResponse response, String user_id,
			String token, String position_id, PageInfo page) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			Map<String, Object> params = new HashMap<String, Object>();
			params.put("user_id", user_id);
			params.put("position_id", position_id);
			params.put("interesting", "1");

			if (!Util.isNull(user_id))// 登录用户
			{
				if (checkService.checkToken(user_id, token, map)) {
					List<ApiU_advert> uAdvertList = this.apiU_advertService.findBonus_AdvertByPostionIDListPage(params,
							page);

					map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
					map.put(Utils.MESSAGE, "获取完成");
					map.put(Utils.DATAS, uAdvertList);
					map.put(Utils.Page, page);
				}
			} else {
				List<ApiU_advert> uAdvertList = this.apiU_advertService.findBonus_AdvertByPostionIDListPage(params,
						page);

				map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
				map.put(Utils.MESSAGE, "获取完成");
				map.put(Utils.DATAS, uAdvertList);
				map.put(Utils.Page, page);
			}

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			map.put(Utils.RESULT, Utils.RESULT_RC_EXCEPTION);
			map.put(Utils.MESSAGE, "系统异常");
		}
		JsonUtil.printJson(response, JsonUtil.objectToJsonStr(map));
	}

	// 更新广告点击数List<ApiU_advert> findBonusU_advertByWords(String keyword)
	@RequestMapping(value = "addAdvert_clickTimes")
	public void addAdvert_clickTimes(HttpServletRequest request, HttpServletResponse response) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			// String cUserId = request.getParameter("user_id");// 用户ID
			// String token = request.getParameter("token");
			String advert_id = request.getParameter("advert_id");
			Map<String, String> params = new HashMap<String, String>();
			// params.put("u_id", cUserId);
			// params.put("token", token);
			params.put("advert_id", advert_id);
			// checkService.checkToken(cUserId, token, map) &&
			if (checkService.checkParams(params, map)) {

				// List<ApiU_advert> uAdvertList =
				// this.apiU_advertService.findPostionU_advert(Integer.valueOf(position_id));
				this.apiU_advertService.addClickTimes(Integer.valueOf(advert_id));

				map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
				map.put(Utils.MESSAGE, "操作完成");
			}

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			map.put(Utils.RESULT, Utils.RESULT_RC_EXCEPTION);
			map.put(Utils.MESSAGE, "系统异常");
		}
		JsonUtil.printJson(response, JsonUtil.object2json(map));
	}

	// 模糊查询红包广告
	@RequestMapping(value = "getBonus_AdvertByWords")
	public void getBonus_AdvertByWords(HttpServletRequest request, HttpServletResponse response, String user_id,
			String token, String keyword, PageInfo page) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			// String cUserId = user_id;
			Map<String, String> params = new HashMap<String, String>();
			if (!Util.isNull(user_id)) {
				params.put("user_id", user_id);
				params.put("token", token);
			}
			params.put("keyword", keyword);

			if (checkService.checkParams(params, map)) {
				if (!Util.isNull(user_id)) {
					if (checkService.checkToken(user_id, token, map)) {
						List<ApiU_advert> uAdvertList = this.apiU_advertService.findBonusU_advertByWords(keyword,
								user_id, page);
						map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
						map.put(Utils.MESSAGE, "获取完成");
						map.put(Utils.DATAS, uAdvertList);
						map.put(Utils.Page, page);
					}
				} else {
					List<ApiU_advert> uAdvertList = this.apiU_advertService.findBonusU_advertByWords(keyword, user_id,
							page);
					map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
					map.put(Utils.MESSAGE, "获取完成");
					map.put(Utils.DATAS, uAdvertList);
					map.put(Utils.Page, page);
				}
			}

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			map.put(Utils.RESULT, Utils.RESULT_RC_EXCEPTION);
			map.put(Utils.MESSAGE, "系统异常");
		}
		JsonUtil.printJson(response, JsonUtil.objectToJsonStr(map));
	}

	// 嗨/喊红包
	// type 8:嗨红包， 17:喊个红包
	@RequestMapping(value = "getCallBonus_Advert")
	public void getCallBonus_Advert(HttpServletRequest request, HttpServletResponse response, String user_id,
			String token, String keyword, String type, PageInfo page) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			Integer position_type = 0;
			if (!Util.isNull(type)) {
				position_type = Integer.valueOf(type);
			}
			else {
				position_type = 8;
			}
			Map<String, String> params = new HashMap<String, String>();

			params.put("user_id", user_id);
			params.put("token", token);
			params.put("keyword", keyword.toUpperCase());

			if (checkService.checkParams(params, map)) {
				if (checkService.checkToken(user_id, token, map)) {
					List<ApiU_advert> uAdvertList = this.apiU_advertService.findCallBonusU_advert(keyword, user_id,
							position_type, page);
					// 添加喊个红包访问次数记录
					//if (!Util.isNull(type) && type.equals("17")) {
					if(position_type == 17){
						if (!Util.isNull(uAdvertList) && uAdvertList.size() > 0) {
							params.clear();
							params.put("keyword", keyword.toUpperCase());
							params.put("type", "2");
							params.put("source", "0");
							this.apiU_stat_keywordsService.SaveBonusVisitCount(params);
						}
					}
					map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
					map.put(Utils.MESSAGE, "获取完成");
					map.put(Utils.DATAS, uAdvertList);
					map.put(Utils.Page, page);
				}
				// }else
				// {
				// List<ApiU_advert> uAdvertList =
				// this.apiU_advertService.findBonusU_advertByWords(keyword,
				// user_id, page);
				// map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
				// map.put(Utils.MESSAGE, "success");
				// map.put(Utils.DATAS, uAdvertList);
				// map.put(Utils.Page, page);
				// }
			}

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			map.put(Utils.RESULT, Utils.RESULT_RC_EXCEPTION);
			map.put(Utils.MESSAGE, "系统异常");
		}
		JsonUtil.printJson(response, JsonUtil.objectToJsonStr(map));
	}

	// 显示红包
	@RequestMapping(value = "getBonus")
	public void getBonus(HttpServletRequest request, HttpServletResponse response, String user_id, String token,
			String bonus_id) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			String cUserId = user_id;
			Map<String, String> params = new HashMap<String, String>();
			params.put("u_id", cUserId);
			params.put("token", token);
			params.put("bonus_id", bonus_id);

			if (checkService.checkToken(cUserId, token, map) && checkService.checkParams(params, map)) {

				ApiU_bonus bonus = this.apiU_bonusService.findBonusByID(Integer.valueOf(bonus_id));

				if (Util.isNull(bonus)) {
					map.put(Utils.RESULT, Utils.RESULT_RC_NO_DATA);
					map.put(Utils.MESSAGE, "no data");
					map.put(Utils.DATAS, null);
				} else {
					map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
					map.put(Utils.MESSAGE, "获取完成");
					map.put(Utils.DATAS, bonus);
				}

			}

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			map.put(Utils.RESULT, Utils.RESULT_RC_EXCEPTION);
			map.put(Utils.MESSAGE, "系统异常");
		}
		JsonUtil.printJson(response, JsonUtil.object2json(map));
	}

	// 打开红包
	@RequestMapping(value = "openBonus")
	public void openBonus(HttpServletRequest request, HttpServletResponse response, String user_id, String token,
			String bonus_id, String get_type) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			String cUserId = user_id;
			Map<String, String> params = new HashMap<String, String>();
			params.put("u_id", cUserId);
			params.put("token", token);
			params.put("bonus_id", bonus_id);
			params.put("get_type", get_type);

			if (checkService.checkToken(cUserId, token, map) && checkService.checkParams(params, map)) {
				ApiU_bonus_detail hasDetail = apiU_bonus_detailService.checkAvailableBonus(bonus_id, user_id);
				if (Util.isNull(hasDetail)) {
					ApiTmember member = apiMemberService.findMemberByCid(Integer.valueOf(cUserId));
					ApiU_bonus_detail bDetail = apiU_bonus_detailService.OpenBonusItem(bonus_id, member, get_type);
					if (Util.isNull(bDetail)) {
						map.put(Utils.RESULT, Utils.RESULT_RC_NO_DATA);
						map.put(Utils.MESSAGE, "红包已被抢光");
					} else {
						map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
						map.put(Utils.MESSAGE, "操作完成");
						map.put(Utils.DATAS, bDetail);
					}
				} else {
					map.put(Utils.RESULT, Utils.RESULT_RC_EXIST);
					map.put(Utils.MESSAGE, "红包已领取，不可重复领取！");
				}
			}

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			map.put(Utils.RESULT, Utils.RESULT_RC_EXCEPTION);
			map.put(Utils.MESSAGE, "系统异常");
		}
		JsonUtil.printJson(response, JsonUtil.object2json(map));
	}

	// 转发红包
	@RequestMapping(value = "forwardBonus")
	public void forwardBonus(HttpServletRequest request, HttpServletResponse response, String user_id, String token,
			String bonus_id, String forward_word, String get_type) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			String cUserId = user_id;
			Map<String, String> params = new HashMap<String, String>();
			params.put("u_id", cUserId);
			params.put("token", token);
			params.put("bonus_id", bonus_id);
			params.put("forward_word", forward_word);

			if (checkService.checkToken(cUserId, token, map) && checkService.checkParams(params, map)) {
				ApiU_bonus_detail hasDetail = apiU_bonus_detailService.checkAvailableBonus(bonus_id, user_id);
				if (Util.isNull(hasDetail)) {
					ApiTmember member = apiMemberService.findMemberByCid(Integer.valueOf(cUserId));
					ApiU_bonus_detail uBonus_detail = apiU_bonus_detailService.ForwardBonusItem(bonus_id, member,
							forward_word, get_type);
					if (Util.isNull(uBonus_detail)) {
						map.put(Utils.RESULT, Utils.RESULT_RC_NO_DATA);
						map.put(Utils.MESSAGE, "红包已抢完，赠送晚了。");
						map.put(Utils.DATAS, null);
					} else {
						map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
						map.put(Utils.MESSAGE, "操作完成");
						map.put(Utils.DATAS, uBonus_detail);
					}
				} else {
					map.put(Utils.RESULT, Utils.RESULT_RC_EXIST);
					map.put(Utils.MESSAGE, "红包已赠送，不可重复赠送！");
				}

			}

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			map.put(Utils.RESULT, Utils.RESULT_RC_EXCEPTION);
			map.put(Utils.MESSAGE, "系统异常");
		}
		JsonUtil.printJson(response, JsonUtil.object2json(map));
	}

	// 显示转发红包
	@RequestMapping(value = "getForwardBonus")
	public void getForwardBonus(HttpServletRequest request, HttpServletResponse response, String user_id, String token,
			String bonus_detail_id) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			// ApiU_bonus_detail getBonus_detailByID(String bonus_detail_id)
			String cUserId = user_id;
			Map<String, String> params = new HashMap<String, String>();
			params.put("u_id", cUserId);
			params.put("token", token);
			params.put("bonus_detail_id", bonus_detail_id);

			if (checkService.checkToken(cUserId, token, map) && checkService.checkParams(params, map)) {

				ApiU_bonus bonus = this.apiU_bonus_detailService.getBonus_ByDetailID(Integer.valueOf(bonus_detail_id));

				if (Util.isNull(bonus)) {
					map.put(Utils.RESULT, Utils.RESULT_RC_NO_DATA);
					map.put(Utils.MESSAGE, "红包不存在。");
					map.put(Utils.DATAS, null);
				} else {
					// 前端判断bonus中bonus_detail的status是否为1，为1则表示已领取过
					map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
					map.put(Utils.MESSAGE, "获取成功");
					map.put(Utils.DATAS, bonus);
				}
			}

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			map.put(Utils.RESULT, Utils.RESULT_RC_EXCEPTION);
			map.put(Utils.MESSAGE, "系统异常");
		}
		JsonUtil.printJson(response, JsonUtil.object2json(map));
	}

	// 打开转发红包
	@RequestMapping(value = "openForwardBonus")
	public void openForwardBonus(HttpServletRequest request, HttpServletResponse response, String user_id, String token,
			String bonus_detail_id) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			String cUserId = user_id;
			Map<String, String> params = new HashMap<String, String>();
			params.put("u_id", cUserId);
			params.put("token", token);
			params.put("bonus_detail_id", bonus_detail_id);

			if (checkService.checkToken(cUserId, token, map) && checkService.checkParams(params, map)) {

				ApiTmember member = apiMemberService.findMemberByCid(Integer.valueOf(cUserId));
				ApiU_bonus_detail bonus_detail = this.apiU_bonus_detailService.openBonus_detailByID(bonus_detail_id,
						member);
				if (Util.isNull(bonus_detail)) {
					map.put(Utils.RESULT, Utils.RESULT_RC_NO_DATA);
					map.put(Utils.MESSAGE, "红包不存在！");
					map.put(Utils.DATAS, null);
				} else {
					map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
					map.put(Utils.MESSAGE, "操作完成");
					map.put(Utils.DATAS, bonus_detail);
				}

			}

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			map.put(Utils.RESULT, Utils.RESULT_RC_EXCEPTION);
			map.put(Utils.MESSAGE, "系统异常");
		}
		JsonUtil.printJson(response, JsonUtil.object2json(map));
	}

	// 嗨/喊个红包中奖列表 1嗨红包 2喊个红包
	@RequestMapping(value = "getBonusRollListWords")
	public void getBonusRollListWords(HttpServletRequest request, HttpServletResponse response, String user_id,
			String token, String type) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			if (Util.isNull(type)) {
				type = "2";
			}
			// if (checkService.checkToken(user_id, token, map) ) {

			List<ApiU_bonus_detail> bonus_detail = this.apiU_bonus_detailService.getRollBonusListPage(type);

			map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
			map.put(Utils.MESSAGE, "获取完成");
			map.put(Utils.DATAS, bonus_detail);

			// }

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			map.put(Utils.RESULT, Utils.RESULT_RC_EXCEPTION);
			map.put(Utils.MESSAGE, "系统异常");
		}
		JsonUtil.printJson(response, JsonUtil.object2json(map));
	}

	// 发布现场红包
	@RequestMapping(value = "publishCallBonus_Advert")
	public void publishCallBonus_Advert(HttpServletRequest request, HttpServletResponse response, String user_id,
			String token, String advert_id, String keyword, String cover, String resources, String logo,
			String blessword) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			Map<String, String> params = new HashMap<String, String>();

			params.put("user_id", user_id);
			params.put("token", token);
			params.put("keyword", keyword);
			params.put("cover", cover);
			params.put("resources", resources);// 多个用:分隔
			params.put("logo", logo);
			params.put("blessword", blessword);
			params.put("advert_id", advert_id);

			if (checkService.checkToken(user_id, token, map)) {
				if (Util.isNull(advert_id)) {
					Integer ret = this.apiU_advertService.createCallBonus_Advert(params);
					if (ret > 0) {
						map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
						map.put(Utils.MESSAGE, "发布完成");
					} else {
						map.put(Utils.RESULT, Utils.RESULT_RC_PARAMTER_ERROR);
						map.put(Utils.MESSAGE, "非企业用户不可创建！");
					}
				} else {
					// Update
					Integer ret = this.apiU_advertService.updateCallBonus_Advert(params);
					if (ret > 0) {
						map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
						map.put(Utils.MESSAGE, "发布完成");
					} else {
						map.put(Utils.RESULT, Utils.RESULT_RC_PARAMTER_ERROR);
						map.put(Utils.MESSAGE, "参数错误！");
					}
				}
			}

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			map.put(Utils.RESULT, Utils.RESULT_RC_EXCEPTION);
			map.put(Utils.MESSAGE, "系统异常");
		}
		JsonUtil.printJson(response, JsonUtil.object2json(map));
	}

	// 发布现场红包确认
	@RequestMapping(value = "comfirmCallBonus_Advert")
	public void comfirmCallBonus_Advert(HttpServletRequest request, HttpServletResponse response, String user_id,
			String token, String advert_id, String summy, String count, String title, String range, String longitude,
			String latitude) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			Map<String, String> params = new HashMap<String, String>();

			params.put("user_id", user_id);
			params.put("token", token);
			params.put("advert_id", advert_id);
			params.put("summy", summy);
			params.put("count", count);
			params.put("title", title);
			params.put("longitude", longitude);
			params.put("latitude", latitude);
			params.put("range", range);

			if (checkService.checkParams(params, map)) {
				if (checkService.checkToken(user_id, token, map)) {
					map.clear();
					String ordernum = this.apiU_advertService.comfirmCallBonus_Advert(params);
					map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
					map.put(Utils.MESSAGE, "发布完成");
					map.put(Utils.DATAS, ordernum);
					// if(ret > 0)
					// {
					// map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
					// map.put(Utils.MESSAGE, "success");
					// }
					// else {
					// map.put(Utils.RESULT, Utils.RESULT_RC_PARAMTER_ERROR);
					// map.put(Utils.MESSAGE, "发布现场红包广告失败！");
					// }
				}
			}

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			// map.put(Utils.RESULT, Utils.RESULT_RC_EXCEPTION);
			// map.put(Utils.MESSAGE, "系统异常");
			map.put(Utils.RESULT, Utils.RESULT_RC_PARAMTER_ERROR);
			map.put(Utils.MESSAGE, "发布现场红包广告失败！");
		}
		JsonUtil.printJson(response, JsonUtil.object2json(map));
	}

	// 现场红包广告列表
	@RequestMapping(value = "callBonus_AdvertList")
	public void callBonus_AdvertList(HttpServletRequest request, HttpServletResponse response, String user_id,
			String token, PageInfo page) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		try {

			if (checkService.checkToken(user_id, token, map)) {
				List<ApiU_advert> advert_list = this.apiU_advertService.findCallBonusU_advertByUserID(user_id, page);

				map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
				map.put(Utils.MESSAGE, "获取完成");
				map.put(Utils.DATAS, advert_list);
				map.put(Utils.Page, page);
			}

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			map.put(Utils.RESULT, Utils.RESULT_RC_EXCEPTION);
			map.put(Utils.MESSAGE, "系统异常");
		}
		JsonUtil.printJson(response, JsonUtil.objectToJsonStr(map));
	}

	// 红包排行榜
	@RequestMapping(value = "getBonusRankingList")
	public void getBonusRankingList(HttpServletRequest request, HttpServletResponse response, String user_id,
			String token, String bonus_id, String type, PageInfo page) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		Map<String, Object> params = new HashMap<String, Object>();
		try {

			if (checkService.checkToken(user_id, token, map)) {
				params.put("user_id", user_id);
				params.put("bonus_id", bonus_id);

				// <ApiU_bonus_detail> bonus_list =
				// this.apiU_bonus_detailService.getBounusRankingList(params,
				// page);
				List<ApiU_bonus_detail> bonus_list = this.apiU_bonus_detailService.getBounusRankingList(params, type,
						page);
				map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
				map.put(Utils.MESSAGE, "获取完成");
				map.put(Utils.DATAS, bonus_list);
				map.put(Utils.Page, page);
			}

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			map.put(Utils.RESULT, Utils.RESULT_RC_EXCEPTION);
			map.put(Utils.MESSAGE, "系统异常");
		}
		JsonUtil.printJson(response, JsonUtil.object2json(map));
	}

	// 发布现场红包(全部)
	@RequestMapping(value = "publishCallBonus_Advert_Full")
	public void publishCallBonus_Advert_Full(HttpServletRequest request, HttpServletResponse response, String user_id,
			String token, String keyword, String cover, String blessword, String summy, String count, String range,
			String longitude, String latitude) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			Map<String, String> params = new HashMap<String, String>();

			params.put("user_id", user_id);
			params.put("token", token);

			if (checkService.checkToken(user_id, token, map)) {
				params.put("keyword", keyword.replaceAll(" ", ":"));
				params.put("cover", cover);
				params.put("blessword", blessword);
				params.put("summy", summy);
				params.put("count", count);
				params.put("longitude", longitude);
				params.put("latitude", latitude);
				params.put("range", range);
				String order_no = this.apiU_advertService.createCallBonus_Advert_Full(params);
				if (order_no.length() > 0) {
					map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
					map.put(Utils.MESSAGE, "发布完成");
					map.put("order_no", order_no);
				} else {
					map.put(Utils.RESULT, Utils.RESULT_RC_PARAMTER_ERROR);
					map.put(Utils.MESSAGE, "发布现场红包失败！");
					map.put("order_no", order_no);
				}
			}

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			map.put(Utils.RESULT, Utils.RESULT_RC_EXCEPTION);
			map.put(Utils.MESSAGE, "系统异常");
		}
		JsonUtil.printJson(response, JsonUtil.object2json(map));
	}

	// 发现场红包继续支付
	@RequestMapping(value = "publishCallBonus_Advert_Repay")
	public void publishCallBonus_Advert_Repay(HttpServletRequest request, HttpServletResponse response, String user_id,
			String token, String advert_id) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			Map<String, String> params = new HashMap<String, String>();

			params.put("user_id", user_id);
			params.put("token", token);

			if (checkService.checkToken(user_id, token, map)) {
				// repay
				params.put("advert_id", advert_id);
				ApiU_advert advert = this.apiU_advertService.updateCallBonus_Advert_Repay(params);

				map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
				map.put(Utils.MESSAGE, "操作完成");
				map.put(Utils.DATAS, advert);
			}

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			map.put(Utils.RESULT, Utils.RESULT_RC_EXCEPTION);
			map.put(Utils.MESSAGE, "系统异常");
		}
		JsonUtil.printJson(response, JsonUtil.object2json(map));
	}

	// 取消现场红包发布(未支付状态)
	@RequestMapping(value = "cancelCallBonus_Advert")
	public void cancelCallBonus_Advert(HttpServletRequest request, HttpServletResponse response, String user_id,
			String token, String advert_id) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			Map<String, String> params = new HashMap<String, String>();

			params.put("user_id", user_id);
			params.put("token", token);

			if (checkService.checkToken(user_id, token, map)) {
				// cancel
				params.put("advert_id", advert_id);
				int ret = this.apiU_advertService.cancelCallBonus_Advert(params);
				if (ret > 0) {
					map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
					map.put(Utils.MESSAGE, "操作完成");
				} else {
					map.put(Utils.RESULT, Utils.RESULT_RC_PARAMTER_ERROR);
					map.put(Utils.MESSAGE, "取消现场红包失败！");
				}
			}

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			map.put(Utils.RESULT, Utils.RESULT_RC_EXCEPTION);
			map.put(Utils.MESSAGE, "系统异常");
		}
		JsonUtil.printJson(response, JsonUtil.object2json(map));
	}

	// 发布普通红包
	@RequestMapping(value = "publicNormalBonus")
	public void publicNormalBonus(HttpServletRequest request, HttpServletResponse response, String user_id,
			String token, String count, String summy, String blessword) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			Map<String, String> params = new HashMap<String, String>();

			params.put("user_id", user_id);
			params.put("count", count);
			params.put("summy", summy);
			params.put("blessword", blessword);

			if (checkService.checkParams(params, map)) {
				if (checkService.checkToken(user_id, token, map)) {

					map = this.apiU_advertService.createNomalBonus(params);
					// if (!Util.isNull(bonus)) {
					// map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
					// map.put(Utils.MESSAGE, "success");
					// map.put(Utils.DATAS, bonus);
					// } else {
					// map.put(Utils.RESULT, Utils.RESULT_RC_PARAMTER_ERROR);
					// map.put(Utils.MESSAGE, "发布普通红包失败！");
					// }
				}
			}

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			map.put(Utils.RESULT, Utils.RESULT_RC_EXCEPTION);
			map.put(Utils.MESSAGE, "系统异常");
		}
		JsonUtil.printJson(response, JsonUtil.object2json(map));
	}

	// 显示普通红包
	@RequestMapping(value = "getNormalBonus")
	public void getNormalBonus(HttpServletRequest request, HttpServletResponse response, String user_id, String token,
			String bonus_id) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			String cUserId = user_id;
			Map<String, String> params = new HashMap<String, String>();
			params.put("u_id", cUserId);
			params.put("token", token);
			params.put("bonus_id", bonus_id);

			if (checkService.checkToken(cUserId, token, map) && checkService.checkParams(params, map)) {

				ApiU_bonus bonus = this.apiU_bonusService.findNormalBonusHis(bonus_id);
				// .findU_bonusById(Integer.valueOf(bonus_id));

				Integer isExist = 0;
				if (Util.isNull(bonus)) {
					map.put(Utils.RESULT, Utils.RESULT_RC_PARAMTER_ERROR);
					map.put(Utils.MESSAGE, "红包不存在！");
				} else {// 红包存在

					ApiTmember member = apiMemberService.findMemberByCid(Integer.valueOf(bonus.getUser_id()));
					bonus.setLogo(member.getCavatar());
					bonus.setCorp_sname(member.getCnick());

					// 检查是否领取过红包
					ApiU_bonus_detail hasDetail = apiU_bonus_detailService.checkAvailableBonus(bonus_id, user_id);
					if (!Util.isNull(hasDetail)) {
						// 领过
						isExist = 1;
					}

					map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
					map.put(Utils.MESSAGE, "操作完成");
					map.put(Utils.DATAS, bonus);
					map.put("isGot", isExist);
				}

			}

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			map.put(Utils.RESULT, Utils.RESULT_RC_EXCEPTION);
			map.put(Utils.MESSAGE, "系统异常");
		}
		JsonUtil.printJson(response, JsonUtil.object2json(map));
	}

	// 显示已抢过的普通红包记录
	@RequestMapping(value = "getNormalBonusHis")
	public void getNormalBonusHis(HttpServletRequest request, HttpServletResponse response, String user_id,
			String token, String bonus_id) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			String cUserId = user_id;
			Map<String, String> params = new HashMap<String, String>();
			params.put("u_id", cUserId);
			params.put("token", token);
			params.put("bonus_id", bonus_id);

			if (checkService.checkToken(cUserId, token, map) && checkService.checkParams(params, map)) {

				ApiU_bonus bonus = this.apiU_bonusService.findNormalBonusHis(bonus_id);

				if (Util.isNull(bonus)) {
					map.put(Utils.RESULT, Utils.RESULT_RC_PARAMTER_ERROR);
					map.put(Utils.MESSAGE, "红包不存在！");
				} else {

					map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
					map.put(Utils.MESSAGE, "操作完成");
					map.put(Utils.DATAS, bonus);
				}

			}

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			map.put(Utils.RESULT, Utils.RESULT_RC_EXCEPTION);
			map.put(Utils.MESSAGE, "系统异常");
		}
		JsonUtil.printJson(response, JsonUtil.object2json(map));
	}

	// 打开普通红包
	@RequestMapping(value = "openNormalBonus")
	public void openNormalBonus(HttpServletRequest request, HttpServletResponse response, String user_id, String token,
			String bonus_id) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			String cUserId = user_id;
			Map<String, String> params = new HashMap<String, String>();
			params.put("u_id", cUserId);
			params.put("token", token);
			params.put("bonus_id", bonus_id);

			if (checkService.checkToken(cUserId, token, map) && checkService.checkParams(params, map)) {

				ApiU_bonus bonus = this.apiU_bonusService.findNormalBonusHis(bonus_id);
				ApiTmember pub_member = apiMemberService.findMemberByCid(Integer.valueOf(bonus.getUser_id()));
				bonus.setLogo(pub_member.getCavatar());
				bonus.setCorp_sname(pub_member.getCnick());

				if (Util.isNull(bonus)) {
					map.put(Utils.RESULT, Utils.RESULT_RC_PARAMTER_ERROR);
					map.put(Utils.MESSAGE, "红包不存在！");
				} else {
					ApiU_bonus_detail hasDetail = apiU_bonus_detailService.checkAvailableBonus(bonus_id, user_id);
					if (Util.isNull(hasDetail)) { // 没有领过红包

						ApiTmember member = apiMemberService.findMemberByCid(Integer.valueOf(cUserId));

						ApiU_bonus_detail bDetail = apiU_bonus_detailService.OpenBonusItem(bonus_id, member, "0");

						// 获取最近记录(若更新了最佳手气)
						bonus = this.apiU_bonusService.findNormalBonusHis(bonus_id);

						bonus.setLogo(pub_member.getCavatar());
						bonus.setCorp_sname(pub_member.getCnick());
					}

					map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
					map.put(Utils.MESSAGE, "操作成功！");
					map.put(Utils.DATAS, bonus);
				}
			}

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			map.put(Utils.RESULT, Utils.RESULT_RC_EXCEPTION);
			map.put(Utils.MESSAGE, "系统异常");
		}
		JsonUtil.printJson(response, JsonUtil.object2json(map));
	}
	
	// 收到的红包 头部
	@RequestMapping(value = "getNormalBonusListHeader")
	public void getNormalBonusListHeader(HttpServletRequest request, HttpServletResponse response, String user_id, String token) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			if (checkService.checkToken(user_id, token, map)) {

				String header_info = this.apiU_bonus_detailService.getReceiveNormalBonusListHeader(user_id);
				String[] headers = header_info.split(":");
				map.put("Received_money", headers[0]);
				map.put("Received_count", headers[1]);
				map.put("Max_count", headers[2]);

				map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
				map.put(Utils.MESSAGE, "操作成功！");
			}

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			map.put(Utils.RESULT, Utils.RESULT_RC_EXCEPTION);
			map.put(Utils.MESSAGE, "系统异常");
		}
		JsonUtil.printJson(response, JsonUtil.object2json(map));
	}
	
	// 收到的红包列表
	@RequestMapping(value = "getNormalBonusList")
	public void getNormalBonusList(HttpServletRequest request, HttpServletResponse response, 
			String user_id, String token, PageInfo page) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			if (checkService.checkToken(user_id, token, map)) {

				
				List<ApiU_bonus_detail> detail_list = this.apiU_bonus_detailService.getReceiveNormalBonusList(user_id, page);
				map.put(Utils.DATAS, detail_list);
				map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
				map.put(Utils.MESSAGE, "操作成功！");
			}

		} catch (Exception e) {
			e.printStackTrace();
			map.put(Utils.RESULT, Utils.RESULT_RC_EXCEPTION);
			map.put(Utils.MESSAGE, "系统异常");
		}
		JsonUtil.printJson(response, JsonUtil.object2json(map));
	}
	
	// 发出的红包 头部
	@RequestMapping(value = "getSendNormalBonusListHeader")
	public void getSendNormalBonusListHeader(HttpServletRequest request, HttpServletResponse response, String user_id, String token) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			if (checkService.checkToken(user_id, token, map)) {

				String header_info = this.apiU_bonus_detailService.getSendNormalBonusListHeader(user_id);
				String[] headers = header_info.split(":");
				map.put("Send_money", headers[0]);
				map.put("Send_count", headers[1]);

				map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
				map.put(Utils.MESSAGE, "操作成功！");
			}

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			map.put(Utils.RESULT, Utils.RESULT_RC_EXCEPTION);
			map.put(Utils.MESSAGE, "系统异常");
		}
		JsonUtil.printJson(response, JsonUtil.object2json(map));
	}
	
	// 发出的红包列表
	@RequestMapping(value = "getSendNormalBonusList")
	public void getSendNormalBonusList(HttpServletRequest request, HttpServletResponse response, 
			String user_id, String token, PageInfo page) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			if (checkService.checkToken(user_id, token, map)) {

				List<ApiU_bonus> bonus = this.apiU_bonusService.getSendNormalBonusList(user_id, page);
				map.put(Utils.DATAS, bonus);
				map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
				map.put(Utils.MESSAGE, "操作成功！");
			}

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			map.put(Utils.RESULT, Utils.RESULT_RC_EXCEPTION);
			map.put(Utils.MESSAGE, "系统异常");
		}
		JsonUtil.printJson(response, JsonUtil.object2json(map));
	}
	
	// 我的嗨红包列表
	@RequestMapping(value = "getMyHaiBonusList")
	public void getMyHaiBonusList(HttpServletRequest request, HttpServletResponse response, 
			String user_id, String token, PageInfo page) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			if (checkService.checkToken(user_id, token, map)) {

				List<ApiU_advert> adv_list = this.apiU_advertService.getMyHaiBonusList(user_id, page);
				map.put(Utils.DATAS, adv_list);
				map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
				map.put(Utils.MESSAGE, "操作成功！");
			}

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			map.put(Utils.RESULT, Utils.RESULT_RC_EXCEPTION);
			map.put(Utils.MESSAGE, "系统异常");
		}
		JsonUtil.printJson(response, JsonUtil.objectToJsonStr(map));
	}
}
